import { Request, RequestConfig } from '../utils/request';
import { UserApi } from './user';
import { PostApi } from './post';
import { CommentApi } from './comment';
import {
  ApiResponse,
  PaginationParams,
  PaginationResult,
  Post,
  Comment,
  UserInfo
} from '../types';

export class BbsApi {
  private request: Request;
  public user: UserApi;
  public post: PostApi;
  public comment: CommentApi;

  constructor(config: RequestConfig) {
    this.request = new Request(config);
    this.user = new UserApi(this.request);
    this.post = new PostApi(this.request);
    this.comment = new CommentApi(this.request);
  }

  // 用户相关接口
  async login(username: string, password: string): Promise<ApiResponse<{ token: string; userInfo: UserInfo }>> {
    return this.request.post('/api/user/login', { username, password });
  }

  async getUserInfo(): Promise<ApiResponse<UserInfo>> {
    return this.request.get('/api/user/info');
  }

  // 帖子相关接口
  async getPosts(params: PaginationParams): Promise<ApiResponse<PaginationResult<Post>>> {
    return this.request.get('/api/posts', { params });
  }

  async getPostDetail(id: number): Promise<ApiResponse<Post>> {
    return this.request.get(`/api/posts/${id}`);
  }

  async createPost(data: { title: string; content: string }): Promise<ApiResponse<Post>> {
    return this.request.post('/api/posts', data);
  }

  async updatePost(id: number, data: { title?: string; content?: string }): Promise<ApiResponse<Post>> {
    return this.request.put(`/api/posts/${id}`, data);
  }

  async deletePost(id: number): Promise<ApiResponse<void>> {
    return this.request.delete(`/api/posts/${id}`);
  }

  // 评论相关接口
  async getComments(postId: number, params: PaginationParams): Promise<ApiResponse<PaginationResult<Comment>>> {
    return this.request.get(`/api/posts/${postId}/comments`, { params });
  }

  async createComment(postId: number, content: string): Promise<ApiResponse<Comment>> {
    return this.request.post(`/api/posts/${postId}/comments`, { content });
  }

  async deleteComment(postId: number, commentId: number): Promise<ApiResponse<void>> {
    return this.request.delete(`/api/posts/${postId}/comments/${commentId}`);
  }
} 
 